const mongoose = require('mongoose')

let FoodTypeSchema = new mongoose.Schema({
    // type name
    typeName: {
        unique: true,
        type: String
    },

    meta: {         //date note
        createAt: {
            type: Date,
            default: Date.now()
        },
        updateAt: {
            type: Date,
            default: Date.now()
        }
    }
})

FoodTypeSchema.pre('save', function (next) {
    if (this.isNew) {
        this.meta.createAt = this.meta.updateAt = Date.now()
    } else {
        this.meta.updateAt = Date.now()
    }
    next()
})

FoodTypeSchema.statics = {
    findById: function (id, cb) {
        return this
            .findOne({ _id: id })
            .exec(cb)
    }
}

module.exports = FoodTypeSchema